package edu.pku.group6.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import edu.pku.group6.entities.*;

/**
 * Data access object (DAO) for domain model class Enterprise.
 * 
 * @see edu.pku.group6.dao.Enterprise
 * @author MyEclipse Persistence Tools
 */

public class EnterpriseDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(EnterpriseDAO.class);

	// property constants
	public static final String USER_ID = "userId";

	public static final String NAME = "name";

	public static final String INFO = "info";

	public static final String NEWS = "news";

	public static final String PERSON = "person";

	public static final String ASSET = "asset";

	public static final String MAIN_PRODUCT = "mainProduct";

	public static final String CARD = "card";

	public static final String CULTURE = "culture";

	public static final String TPYE = "tpye";

	public static final String REGISTERATION = "registeration";

	public static final String RECOMMANDED = "recommanded";

	public void save(Enterprise transientInstance) {
		log.debug("saving Enterprise instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Enterprise persistentInstance) {
		log.debug("deleting Enterprise instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Enterprise findById(java.lang.Integer id) {
		log.debug("getting Enterprise instance with id: " + id);
		try {
			Enterprise instance = (Enterprise) getSession().get(
					"edu.pku.group6.entities.Enterprise", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Enterprise instance) {
		log.debug("finding Enterprise instance by example");
		try {
			List results = getSession().createCriteria(
					"edu.pku.group6.entities.Enterprise").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Enterprise instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Enterprise as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByInfo(Object info) {
		return findByProperty(INFO, info);
	}

	public List findByNews(Object news) {
		return findByProperty(NEWS, news);
	}

	public List findByPerson(Object person) {
		return findByProperty(PERSON, person);
	}

	public List findByAsset(Object asset) {
		return findByProperty(ASSET, asset);
	}

	public List findByMainProduct(Object mainProduct) {
		return findByProperty(MAIN_PRODUCT, mainProduct);
	}

	public List findByCard(Object card) {
		return findByProperty(CARD, card);
	}

	public List findByCulture(Object culture) {
		return findByProperty(CULTURE, culture);
	}

	public List findByTpye(Object tpye) {
		return findByProperty(TPYE, tpye);
	}

	public List findByRegisteration(Object registeration) {
		return findByProperty(REGISTERATION, registeration);
	}

	public List findByRecommanded(Object recommanded) {
		return findByProperty(RECOMMANDED, recommanded);
	}

	public List findAll() {
		log.debug("finding all Enterprise instances");
		try {
			String queryString = "from Enterprise";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Enterprise merge(Enterprise detachedInstance) {
		log.debug("merging Enterprise instance");
		try {
			Enterprise result = (Enterprise) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Enterprise instance) {
		log.debug("attaching dirty Enterprise instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Enterprise instance) {
		log.debug("attaching clean Enterprise instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}